<?php

namespace app\controller\admin\calendar;

use app\model\admin\calendar\CalendarContent;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Career
 * @package app\controller\admin\ah
 */
class Content extends Backend
{
    protected $model;
    public $orderRule=['sort' => 'DESC','id'=>'ASC'];
    protected $noNeedAuth = ['index', 'info'];
    protected $typeName = [
      1=>"视频类型",
      2=>"文章类型",
    ];
    protected $categoryName = [
        1=>"活动类别",
        2=>"资讯类别",
    ];

    public function initialize()
    {
        $this->model = new CalendarContent();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    //查看详情
    public function info()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        if(isset($info->icon)){
            $info->icon = imgUrlToAbsolute($info->icon);
        }
        if(isset($info->picture)){
            $info->picture = imgUrlToAbsolute($info->picture);
        }
        return $this->success('获取成功', $info);
    }

    public function index()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        } else {
            $limit = $this->request->param('limit', 10);
            $data  = $this->model->with(['oneModule','oneLabel'])->where($where)->order($order)->paginate($limit)->toArray();
            foreach ($data['data'] as &$item){
                if(isset($item['oneModule']['name'])){
                    $item['oneModule'] = $item['oneModule']['name'];
                }
                if(isset($item['oneLabel']['name'])){
                    $item['oneLabel'] = $item['oneLabel']['name'];
                }
                if(isset($this->typeName[$item['type']])){
                    $item['typeName'] =$this->typeName[$item['type']];
                }else{
                    $item['typeName'] = "";
                }
                if(isset($this->categoryName[$item['category_id']])){
                    $item['categoryName'] =$this->categoryName[$item['category_id']];
                }else{
                    $item['categoryName'] = "";
                }
                if(empty($item['start_time'])){
                    $item['time'] = "";
                }else{
                    $item["time"] = date("Y/m/d",strtotime($item['start_time']))."-".date("Y/m/d",strtotime($item['end_time']));
                }
            }
        }
        return $this->success('数据获取成功', $data);
    }

    //添加
    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());
        try{
            IF(isset($post['icon'])&&!empty($post['icon'])){
                $post['icon'] = getRelativeUrl($post['icon']);
            }
            if(isset($post['time'])){
                $listTime = explode(" - ",$post['time']);
                if(count($listTime)>1){
                    $post['start_time'] = $listTime[0];
                    $post['end_time'] = $listTime[1];
                }
            }
            if ($this->model->create($post)) {
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    //编辑
    public function edit()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        if (!$info) {
            return $this->error('ID参数错误');
        }
        $post = CommonFun::filterPostData($this->request->post());
        IF(isset($post->icon)&&!empty($post->icon)){
            $post->icon = getRelativeUrl( $post->icon);
        }
        foreach ($post as $k => $v) {
            $info->$k = $v;
        }
        if(isset($post['time'])){
            $listTime = explode(" - ",$post['time']);
            if(count($listTime)>1){
                $info->start_time = $listTime[0];
                $info->end_time = $listTime[1];
            }
        }
        try {
            $updateRes = $info->save();
            if ($updateRes) {
                $this->clearCache();
                return $this->success('编辑成功');
            } else {
                return $this->error('编辑失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    public function setSubscribeStatus()
    {
        $id       = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['subscribe_status'] = $fieldVal;

        try {
            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }

            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    public function setLabelStatus()
    {
        $id       = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['label_status'] = $fieldVal;

        try {
            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }

            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


}